﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmdLineParserModule;
using cmdLineParserModule.HelperClasses;

namespace cmdLineParserModule
{
    /// <summary>
    /// Option class for marking switcher attribute, usable with 'bool' only
    /// </summary>
    public class optionSwitcherAttribute : optionAttribute
    {
        /// <summary>
        /// Fills field with true/false value
        /// </summary>
        /// <param name="argumentClass">Object containing the field</param>
        /// <param name="choiceToken">Token containing choice</param>
        /// <param name="parameterToken">Token containing parameter</param>
        internal override void parse(object argumentClass, cmdLineToken choiceToken, cmdLineToken parameterToken)
 	    {
            if (choiceToken == null)
            {
                if (this.choiceRequired)
                    throw new WrongUsageException("Choice {0} is required, but as a switcher it can not be (would be always true)", this.command);
                else
                {
                    this.field.SetValue(argumentClass, false);
                }
            }
            else
            {
                choiceToken.isUsed = true;
                this.field.SetValue(argumentClass, true);
            }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public optionSwitcherAttribute()
        {
            // this choice can never be required (it has no sense)
            this.choiceRequired = false;
        }

        /// <summary>
        /// Type of property marked with this class, must be bool
        /// </summary>
        /// <returns>Type of property accepted by parser</returns>
        internal override Type typeAccepted()
        {
            return typeof(bool);
        }

        /// <summary>
        /// Custom checking, choice switcher can never be required.
        /// </summary>
        protected override void customCheck()
        {
            base.customCheck();
            if (this.choiceRequired)
                throw new WrongUsageException("Choice {0} is required, but as a switcher it can not be (would be always true)", this.command);
        }

        /// <summary>
        /// Returns usage text printed in parser.printUsage()
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public override string getUsageText(string cmd)
        {
            return ChoiceTextManager.addChoicePrefix(cmd);
        }
    }
}
